﻿using log4net;
using log4net.Config;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace OpenApi
{
	public class MyLogger
	{
		public static readonly ILog Default = LogManager.GetLogger("openapi");
		static ILog DebugLog = LogManager.GetLogger("openapi.debug");
		static ILog InfoLog = LogManager.GetLogger("openapi.info");
		static ILog WarnLog = LogManager.GetLogger("openapi.warn");
		static ILog ErrorLog = LogManager.GetLogger("openapi.error");
		static ILog FatalLog = LogManager.GetLogger("openapi.fatal");

		/// <summary>
		/// 日志JSON序列化设置
		/// </summary>
		public static JsonSerializerSettings LogJss { get; private set; }

		static MyLogger()
		{
			XmlConfigurator.Configure();
			LogJss = new JsonSerializerSettings()
			{
				ReferenceLoopHandling = ReferenceLoopHandling.Ignore
			};
		}

		/// <summary>
		/// 调试
		/// </summary>
		/// <param name="message"></param>
		public static void Debug(object message)
		{
			DebugLog.Debug(message);
		}

		/// <summary>
		/// 信息
		/// </summary>
		/// <param name="message"></param>
		public static void Info(object message)
		{
			InfoLog.Info(message);
		}

		/// <summary>
		/// 警告
		/// </summary>
		/// <param name="message"></param>
		public static void Warn(object message)
		{
			WarnLog.Warn(message);
		}

		/// <summary>
		/// 错误
		/// </summary>
		/// <param name="message"></param>
		public static void Error(object message)
		{
			ErrorLog.Error(message);
		}

		/// <summary>
		/// 错误
		/// </summary>
		/// <param name="message"></param>
		/// <param name="ex"></param>
		public static void Error(object message, Exception ex)
		{
			FatalLog.Error(message, ex);
		}

		/// <summary>
		/// 致命
		/// </summary>
		/// <param name="message"></param>
		public static void Fatal(object message)
		{
			FatalLog.Fatal(message);
		}

		/// <summary>
		/// 致命
		/// </summary>
		/// <param name="message"></param>
		/// <param name="ex"></param>
		public static void Fatal(object message, Exception ex)
		{
			FatalLog.Fatal(message, ex);
		}

		/// <summary>
		/// 将日志输出信息序列化
		/// </summary>
		/// <param name="message"></param>
		/// <returns></returns>
		public static string Json(OperationDescription message)
		{
			try
			{
				return JsonConvert.SerializeObject(
					new
					{
						MethodName = message.Name,
						MethodParams = message.SyncMethod.GetParameters().Select(p => p.Name).ToList()
					}

					, LogJss);
			}
			catch
			{
				return "MyLogger日志序列化出错-OperationDescription";
			}

		}

		/// <summary>
		/// 将日志输出信息序列化
		/// </summary>
		/// <param name="message"></param>
		/// <returns></returns>
		public static string Json(object message)
		{
			try
			{
				return JsonConvert.SerializeObject(message, LogJss);
			}
			catch
			{
				return "MyLogger日志序列化出错-Object";
			}
		}
	}
}
